package com.ztesoft.huangfu.learning.springboot.springbootaop.validator.constraintvalidators;




import com.ztesoft.huangfu.learning.springboot.springbootaop.validator.constraints.IntegerEnum;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import static org.apache.commons.lang.ArrayUtils.contains;


/**
 * Created by ZaoSheng on 2016/11/16.
 */
public class IntegerEnumValdator implements ConstraintValidator<IntegerEnum, Integer> {
    private int[] type;
    @Override
    public void initialize(IntegerEnum constraintAnnotation) {
        this.type = constraintAnnotation.type();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if(value == null  || type == null || type.length == 0){
            return true;
        }
        return contains(type, value);
    }

}